<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>Genetic Query Optimization (GEQO) in PostgreSQL</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 9.1.2 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="Genetic Query Optimizer"
HREF="geqo.html"><LINK
REL="PREVIOUS"
TITLE="Genetic Algorithms"
HREF="geqo-intro2.html"><LINK
REL="NEXT"
TITLE="Further Reading"
HREF="geqo-biblio.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2011-12-01T22:07:59"></HEAD
><BODY
CLASS="SECT1"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
><A
HREF="index.html"
>PostgreSQL 9.1.2 Documentation</A
></TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
TITLE="Genetic Algorithms"
HREF="geqo-intro2.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="geqo.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
>Chapter 51. Genetic Query Optimizer</TD
><TD
WIDTH="20%"
ALIGN="right"
VALIGN="top"
><A
TITLE="Further Reading"
HREF="geqo-biblio.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="GEQO-PG-INTRO"
>51.3. Genetic Query Optimization (<ACRONYM
CLASS="ACRONYM"
>GEQO</ACRONYM
>) in PostgreSQL</A
></H1
><P
>    The <ACRONYM
CLASS="ACRONYM"
>GEQO</ACRONYM
> module approaches the query
    optimization problem as though it were the well-known traveling salesman
    problem (<ACRONYM
CLASS="ACRONYM"
>TSP</ACRONYM
>).
    Possible query plans are encoded as integer strings. Each string
    represents the join order from one relation of the query to the next.
    For example, the join tree
</P><PRE
CLASS="LITERALLAYOUT"
>   /\
  /\ 2
 /\ 3
4  1</PRE
><P>
    is encoded by the integer string '4-1-3-2',
    which means, first join relation '4' and '1', then '3', and
    then '2', where 1, 2, 3, 4 are relation IDs within the
    <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> optimizer.
   </P
><P
>    Specific characteristics of the <ACRONYM
CLASS="ACRONYM"
>GEQO</ACRONYM
>
    implementation in <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>
    are:

    <P
></P
></P><UL
COMPACT="COMPACT"
><LI
STYLE="list-style-type: disc"
><P
>       Usage of a <I
CLASS="FIRSTTERM"
>steady state</I
> <ACRONYM
CLASS="ACRONYM"
>GA</ACRONYM
> (replacement of the least fit
       individuals in a population, not whole-generational replacement)
       allows fast convergence towards improved query plans. This is
       essential for query handling with reasonable time;
      </P
></LI
><LI
STYLE="list-style-type: disc"
><P
>       Usage of <I
CLASS="FIRSTTERM"
>edge recombination crossover</I
>
       which is especially suited to keep edge losses low for the
       solution of the <ACRONYM
CLASS="ACRONYM"
>TSP</ACRONYM
> by means of a
       <ACRONYM
CLASS="ACRONYM"
>GA</ACRONYM
>;
      </P
></LI
><LI
STYLE="list-style-type: disc"
><P
>       Mutation as genetic operator is deprecated so that no repair
       mechanisms are needed to generate legal <ACRONYM
CLASS="ACRONYM"
>TSP</ACRONYM
> tours.
      </P
></LI
></UL
><P>
   </P
><P
>    Parts of the <ACRONYM
CLASS="ACRONYM"
>GEQO</ACRONYM
> module are adapted from D. Whitley's
    Genitor algorithm.
   </P
><P
>    The <ACRONYM
CLASS="ACRONYM"
>GEQO</ACRONYM
> module allows
    the <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> query optimizer to
    support large join queries effectively through
    non-exhaustive search.
   </P
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN93796"
>51.3.1. Generating Possible Plans with <ACRONYM
CLASS="ACRONYM"
>GEQO</ACRONYM
></A
></H2
><P
>    The <ACRONYM
CLASS="ACRONYM"
>GEQO</ACRONYM
> planning process uses the standard planner
    code to generate plans for scans of individual relations.  Then join
    plans are developed using the genetic approach.  As shown above, each
    candidate join plan is represented by a sequence in which to join
    the base relations.  In the initial stage, the <ACRONYM
CLASS="ACRONYM"
>GEQO</ACRONYM
>
    code simply generates some possible join sequences at random.  For each
    join sequence considered, the standard planner code is invoked to
    estimate the cost of performing the query using that join sequence.
    (For each step of the join sequence, all three possible join strategies
    are considered; and all the initially-determined relation scan plans
    are available.  The estimated cost is the cheapest of these
    possibilities.)  Join sequences with lower estimated cost are considered
    <SPAN
CLASS="QUOTE"
>"more fit"</SPAN
> than those with higher cost.  The genetic algorithm
    discards the least fit candidates.  Then new candidates are generated
    by combining genes of more-fit candidates &mdash; that is, by using
    randomly-chosen portions of known low-cost join sequences to create
    new sequences for consideration.  This process is repeated until a
    preset number of join sequences have been considered; then the best
    one found at any time during the search is used to generate the finished
    plan.
   </P
><P
>    This process is inherently nondeterministic, because of the randomized
    choices made during both the initial population selection and subsequent
    <SPAN
CLASS="QUOTE"
>"mutation"</SPAN
> of the best candidates.  To avoid surprising changes
    of the selected plan, each run of the GEQO algorithm restarts its
    random number generator with the current <A
HREF="runtime-config-query.html#GUC-GEQO-SEED"
>geqo_seed</A
>
    parameter setting.  As long as <TT
CLASS="VARNAME"
>geqo_seed</TT
> and the other
    GEQO parameters are kept fixed, the same plan will be generated for a
    given query (and other planner inputs such as statistics).  To experiment
    with different search paths, try changing <TT
CLASS="VARNAME"
>geqo_seed</TT
>.
   </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="GEQO-FUTURE"
>51.3.2. Future Implementation Tasks for
    <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> <ACRONYM
CLASS="ACRONYM"
>GEQO</ACRONYM
></A
></H2
><P
>      Work is still needed to improve the genetic algorithm parameter
      settings.
      In file <TT
CLASS="FILENAME"
>src/backend/optimizer/geqo/geqo_main.c</TT
>,
      routines
      <CODE
CLASS="FUNCTION"
>gimme_pool_size</CODE
> and <CODE
CLASS="FUNCTION"
>gimme_number_generations</CODE
>,
      we have to find a compromise for the parameter settings
      to satisfy two competing demands:
      <P
></P
></P><UL
COMPACT="COMPACT"
><LI
><P
>         Optimality of the query plan
        </P
></LI
><LI
><P
>         Computing time
        </P
></LI
></UL
><P>
     </P
><P
>      In the current implementation, the fitness of each candidate join
      sequence is estimated by running the standard planner's join selection
      and cost estimation code from scratch.  To the extent that different
      candidates use similar sub-sequences of joins, a great deal of work
      will be repeated.  This could be made significantly faster by retaining
      cost estimates for sub-joins.  The problem is to avoid expending
      unreasonable amounts of memory on retaining that state.
     </P
><P
>      At a more basic level, it is not clear that solving query optimization
      with a GA algorithm designed for TSP is appropriate.  In the TSP case,
      the cost associated with any substring (partial tour) is independent
      of the rest of the tour, but this is certainly not true for query
      optimization.  Thus it is questionable whether edge recombination
      crossover is the most effective mutation procedure.
     </P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="geqo-intro2.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="geqo-biblio.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Genetic Algorithms</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="geqo.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Further Reading</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>